/**
 * 定义监测仪器管理模块
 */
(function(window, angular, undefined){
	var module = angular.module('water.apps.common.stationInfo.monitorEquipmentManager', ['water.entity.dataCollectionInstrumet','water.entity.monitorEquipment']);
	
	module.controller('commonMonitorEquipmentManagerController',function($rootScope,$scope,$monitorEquipmentService,$monitorStationService){

		/**
		 * 编辑对话框对象
		 */
		$scope.editDialog;
		/**
		 * 详情对话框对象
		 */
		$scope.detailDialog;
		/**
		 * 新增对话框
		 */
		$scope.createDialog;
		/**
		 * 编辑对象
		 */
		$scope.equipmentEdit = {};
		/**
		 * 站点新增对象
		 */
		$scope.equipmentCreate = {};
		/**
		 * 站点详情对象
		 */
		$scope.stationDetail = {};
		/**
		 * 当前选中的站点对象
		 */
		$scope.stationModel={};
		/**
		 * 初始化站点选择数据
		 */
		$scope.stationSelectList = null;
		/**
		 *新建站点数据
		 */
		$scope.stationSelecter;
		/**
		 * 编辑站点数据
		 */
		$scope.stationSelecterEdit;
		/**
		 * 监测仪器数据
		 */
		$scope.equipments=[];
		/**
		 * 监听站点列表点击事件
		 */
		$scope.dataPramChangedBroadCast =$scope.$on('dataPramChanged', function(event, data) {
			$scope.stationModel = data;
			$scope._getEquipmentList();
			$scope._getStationSelectList();
	    });
		/**
		 * 仪器代号修改事件
		 */
		$scope.onEquipmentCodeChange = function(model){
			if(!model){
				return;
			}
			$scope.equipmentEdit.NAME = $scope.stationModel.STATION_NAME + "-" + model.v;
			$scope.equipmentCreate.NAME = $scope.stationModel.STATION_NAME + "-" + model.v;
		}		
		/**
		 * 详情按钮点击事件
		 */
		$scope.onDetailClick = function(data){
			$monitorEquipmentService.getModel(data.GUID).then(function(d){
				$scope.equipmentDetail = d;
				$scope.equipmentModel = data;
				$scope.detailDialog.open();
				console.log("onDetailClick###",d);
				var order = $scope.equipmentDetail.DEVICE_ORDER_LIST;
				$scope.orderName="";
				if(order.length>0) {
					for(var i=0;i<order.length;i++) {
						$scope.orderName+=order[i].ORDER_ID_DESC+",";
					}
					$scope.orderName=$scope.orderName.substring(0,$scope.orderName.length-1);
				}
			});
		}
		/**
		 * 编辑按钮点击事件
		 */
		$scope.onEditClick = function(data){
			$monitorEquipmentService.getModel(data.GUID).then(function(d){
				$scope.equipmentEdit = d;
				$scope.editDialog.open();
			});
		}
		/**
		 * 提交编辑按钮点击事件
		 */
		$scope.onSubmitEditClick = function(data){
			data = $scope.updatePollListStationId(data);
			$monitorEquipmentService.updateModel(data).then(function(){
				$.messager.alert("保存成功","保存成功",'info');
				$scope._getEquipmentList();
				$scope.editDialog.close();
			});
			
		}
		/**
		 * 新增按钮点击事件
		 */
		$scope.onCreateClick = function(){
			$scope.equipmentCreate = {
					MONITOR_POINT:$scope.stationModel.STATION_ID
			};
			$scope.createDialog.open();
		}
		/**
		 * 提交新建按钮点击事件
		 */
		$scope.onSubmitCreateClick = function(data){
			data = $scope.updatePollListStationId(data);
			console.log("$monitorEquipmentService######",data);
			$monitorEquipmentService.createModel(data).then(function(){
				$.messager.alert("保存成功","保存成功",'info');
				$scope.createDialog.close();
				$scope._getEquipmentList();
			});
		}
		/**
		 * 删除仪器
		 */
		$scope.onDeleteClick = function(data){
			$.messager.confirm("确认","确定要删除这条记录吗？",function(r){
				if(r){
					$monitorEquipmentService.deleteModel(data.GUID).then(function(){
						$.messager.alert("删除成功","删除成功",'info');
						$scope._getEquipmentList();
					});
				}
			});
				
		}
		/**
		 * 如果设置了站点污染物，把所有站点污染物的站点id更新
		 */
		$scope.updatePollListStationId=function(data){
			if(data.BC_SITE_POLL_LIST){
				for(var i=0;i<data.BC_SITE_POLL_LIST.length;i++){
					data.BC_SITE_POLL_LIST[i].SITE_ID = data.MONITOR_POINT;
				}  
			}
			return data;
		}
		/**
		 * 获取仪器列表
		 */
		$scope._getEquipmentList=function (){
			$monitorEquipmentService.getList({STATION_ID:$scope.stationModel.STATION_ID}).then(function(d){
				$scope.equipments = d
			});
		}
		/**
		 * 获取站点列表，编辑或者新增的时候需要选择
		 */
		$scope._getStationSelectList = function(){
			$monitorStationService.getDomainList({STATION_ID:$scope.stationModel.STATION_ID}).then(function(d){
				$scope.stationSelectList = d;
			});
		};
		
		
	});
}(window, angular));	